﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSTSCLib;
using file;
using System.Reflection;

namespace DMSMT.Tool
{
    public partial class RDCFrm : Form
    {
        List<List<string>> rdpList = null;

        public RDCFrm()
        {
            InitializeComponent();
        }

        private void RDCFrm_Load(object sender, EventArgs e)
        {
            ExcelReader er = new ExcelReader();
            er.StratRow = 2;
            er.StratCol = 1;
            string path = Assembly.GetExecutingAssembly().Location;
            path = path.Substring(0, path.LastIndexOf("\\") + 1) + "rdp.xls";
            Log.LogText("读取远程服务器列表:\t"+path);
            rdpList = er.read(path);


            //for (int i = 0; i < list.Count; i++)
            //{
            //    List<string> row = list[i];
            //    ListViewItem lvi = new ListViewItem();

            //    //lvi.ImageIndex = i;     //通过与imageList绑定，显示imageList中第i项图标

            //    lvi.Text = "subitem" + i;

            //    for (int j = 0; j < row.Count; i++)
            //    {
            //        lvi.SubItems.Add(row[j]);
            //    }

            //    this.lAll.Items.Add(lvi);
            //}
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
            List<List<string>> list = new List<List<string>>();
            for (int i = 0; i < lRdp.Items.Count; i++)
            {
                ListViewItem item = lRdp.Items[i];
                List<string> l = new List<string>();
                for (int j = 0; j < item.SubItems.Count; j++)
                {
                    l.Add(item.SubItems[j].Text);
                }
                list.Add(l);
                
            }

            RDPMainFrm frm = new RDPMainFrm();
            frm.rdpList = list;
            frm.Show();
            
            //try
            //{
            //    rdp.Server = tbServer.Text;  //乐博网提示  这里是输入服务器的ip地址或者域名
            //    rdp.UserName = tbUserName.Text;   // 这里是输入服务器的登录用户 比如administrator

            //    IMsTscNonScriptable secured = (IMsTscNonScriptable)rdp.GetOcx();
            //    secured.ClearTextPassword = tbPwd.Text;  //这里输入服务器密码
            //    rdp.Connect();
            //}
            //catch (Exception Ex)
            //{
            //    MessageBox.Show("连接错误", "不能连接到远程桌面 " + tbServer.Text + " 错误信息:  " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    // Check if connected before disconnecting
            //    if (rdp.Connected.ToString() == "1")
            //        rdp.Disconnect();
            //}
            //catch (Exception Ex)
            //{
            //    MessageBox.Show("断开连接失败", "不能断开远程桌面连接 " + tbServer.Text + " 错误信息:  " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //}
        }

        private void tbServer_Leave(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tbServer.Text))
            {
                return;
            }
            for (int i = 0; i < rdpList.Count; i++)
            {
                List<string> row = rdpList[i];
                if (row[3].Trim() == tbServer.Text.Trim())
                {
                    tbUserName.Text=row[6];
                    tbPwd.Text = row[7];
                    tbNetName.Text = row[2];
                    break;
                }
            }
        }

        private void btAdd2List_Click(object sender, EventArgs e)
        {
            string userName = tbUserName.Text.Trim();
            string pwd = tbPwd.Text.Trim();
            string server = tbServer.Text.Trim();

            if(string.IsNullOrEmpty(userName)||string.IsNullOrEmpty(pwd)||string.IsNullOrEmpty(server))
            {
                Log.LogText("IP、用户名、密码都不能为空!");
                return;
            }

            addServer(tbNetName.Text,server,userName,pwd);
            //this.lRdp.BeginUpdate();
            //ListViewItem lvi = new ListViewItem();

            ////lvi.ImageIndex = i;     //通过与imageList绑定，显示imageList中第i项图标

            //lvi.Text = tbNetName.Text;
            //lvi.SubItems.Add(server);
            //lvi.SubItems.Add(userName);
            //lvi.SubItems.Add(pwd);
            //this.lRdp.Items.Add(lvi);
            //this.lRdp.EndUpdate(); 

        }

        private void addServer(string text,string server,string u,string p)
        {

            //this.lRdp.BeginUpdate();
            ListViewItem lvi = new ListViewItem();

            lvi.Text = text;
            lvi.SubItems.Add(server);
            lvi.SubItems.Add(u);
            lvi.SubItems.Add(p);
            this.lRdp.Items.Add(lvi);
            //this.lRdp.EndUpdate(); 
        }

        private void addServer(List<string> row)
        {

            //this.lRdp.BeginUpdate();
            ListViewItem lvi = new ListViewItem();

            lvi.Text = row[2];
            lvi.SubItems.Add(row[3]);
            lvi.SubItems.Add(row[6]);
            lvi.SubItems.Add(row[7]);
            this.lRdp.Items.Add(lvi);
            //this.lRdp.EndUpdate(); 
        }

        private void btAddList_Click(object sender, EventArgs e)
        {
            string[] ips = rtbIP.Lines;
            if (ips.Length == 0)
            {
                MessageBox.Show("请输入IP列表");
                return;
            }

            foreach (string ip in ips)
            {
                string ipstr = ip.Trim();
                if (!string.IsNullOrEmpty(ipstr))
                {
                    for (int i = 0; i < rdpList.Count; i++)
                    {
                        List<string> row = rdpList[i];
                        if (row[3].Trim() == ipstr.Trim())
                        { 
                            addServer(row);
                            break;
                        }
                    }



                    //try
                    //{
                    //    string res = Dosearch(ipstr + ":8080");
                    //    Log.LogText(ipstr + "\t" + res);
                    //}
                    //catch (Exception ex)
                    //{
                    //    Log.LogError(ipstr + "\t" + ex.Message);
                    //}
                }
            }
        }

        private void btTest_Click(object sender, EventArgs e)
        {
            RDPTestFrm f = new RDPTestFrm();
            f.ShowDialog();
        }
    }
}
